package com.spr.controller;

import java.util.List;

import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.spr.exception.FacultyNotFound;
import com.spr.model.Faculty;
import com.spr.model.User;
import com.spr.service.FacultyService;
import com.spr.validation.*;

@Controller
@RequestMapping(value="/faculty")
public class FacultyController {

	 @Autowired
     private FacultyService FacultyService;
     
     @Autowired
     private FacultyValidator FacultyValidator;
	
     @InitBinder
     private void initBinder(WebDataBinder binder) {
             binder.setValidator(FacultyValidator);
     }
     
  @RequestMapping(value="/create", method=RequestMethod.GET)
    public ModelAndView newFacultyPage() {
            ModelAndView mav = new ModelAndView("faculty-new", "faculty", new Faculty());
            return mav;
    }
    
    @RequestMapping(value="/create", method=RequestMethod.POST)
    public ModelAndView createFaculty(@ModelAttribute @Valid Faculty faculty, HttpSession session, 
                    BindingResult result,
                    final RedirectAttributes redirectAttributes) {
            
            if (result.hasErrors())
                    return new ModelAndView("faculty-new");
            
            ModelAndView mav = new ModelAndView();
            String message = "New Account "+faculty.getFirstName()+ " " +faculty.getLastName() +" was successfully created.";
            
            User user = (User) session.getAttribute("UserDetails");
            
            faculty.setUserID(user.getUserID());
            
            FacultyService.create(faculty);
            mav.setViewName("redirect:/faculty/list.html");
                            
            redirectAttributes.addFlashAttribute("message", message);        
            return mav;                
    }
    
	@RequestMapping(value="/edit/{id}", method=RequestMethod.GET)
	public ModelAndView editFacultyPage(@PathVariable Integer id){
		ModelAndView mav = new ModelAndView("faculty-edit");
		Faculty faculty = FacultyService.findById(id);
        mav.addObject("faculty", faculty);
        return mav;
	}
	
    @RequestMapping(value="/edit/{id}", method=RequestMethod.POST)
    public ModelAndView editFaculty(@ModelAttribute @Valid Faculty faculty,
                    BindingResult result,
                    @PathVariable Integer id,
                    final RedirectAttributes redirectAttributes) throws FacultyNotFound {
            
            if (result.hasErrors())
                    return new ModelAndView("faculty-edit");
            
            ModelAndView mav = new ModelAndView("redirect:/faculty/list.html");
            String message = "Account was successfully updated.";

            faculty.setFacultyID(id);
            FacultyService.update(faculty);
            
            redirectAttributes.addFlashAttribute("message", message);        
            return mav;
    }
    
	@RequestMapping(value="/view/{id}", method=RequestMethod.GET)
	public ModelAndView viewFacultyPage(@PathVariable Integer id){
		ModelAndView mav = new ModelAndView("faculty-view");
		Faculty faculty = FacultyService.findById(id);
        mav.addObject("faculty", faculty);
        return mav;
	}
	
    @RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
    public ModelAndView deleteFaculty(@PathVariable Integer id,
                    final RedirectAttributes redirectAttributes) throws FacultyNotFound {
            
            ModelAndView mav = new ModelAndView("redirect:/faculty/list.html");                
            
            Faculty faculty = FacultyService.delete(id);  
            String message = "The Account "+faculty.getFirstName()+ " " +faculty.getLastName() +" was successfully deleted.";
            
            redirectAttributes.addFlashAttribute("message", message);
            return mav;
    }
	@RequestMapping(value="/list", method=RequestMethod.GET)
	public ModelAndView listFacultyPage(){
		ModelAndView mav = new ModelAndView("faculty-list");
        List<Faculty> facultyList = FacultyService.findAll();
        mav.addObject("facultyList", facultyList);
        return mav;
	}
}
